package com.yhgarden.bbs.controller.common;

import com.yhgarden.bbs.model.constants.MailConstants;
import com.yhgarden.bbs.model.constants.YhbbsConstants;
import com.yhgarden.bbs.model.enums.StatusCode;
import com.yhgarden.bbs.model.response.BaseResponse;
import com.yhgarden.bbs.service.api.common.MailService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.ExecutorService;

@Controller
@RequestMapping(value = {"/common/mail","*/common/mail"})
public class MailController {

    @Autowired
    private MailService mailService;

    @Autowired
    private ExecutorService executorService;

    @RequestMapping(value = "/{email}/sendRandomCode", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse sendRandomCode(HttpSession session,@PathVariable("email") String email) throws UnsupportedEncodingException {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        //生成六位验证码
        Integer randomCode = RandomUtils.nextInt(100000,999999);
        //写入验证码到session
        session.setAttribute(YhbbsConstants.RANDOM_CODE, randomCode.toString());
        String content = String.format(MailConstants.EMAIL_RANDOM_CODE_CONTENT,randomCode);
//        executorService.submit(()->{
//            mailService.sendSimpleMail(email, MailConstants.EMAIL_RANDOM_CODE_SUBJECT,content);
//        });
        System.out.println("=====================randomCode==================:"+randomCode);
        return response;
    }

}
